<template>
  <div class="deploy-container">
    <ContentWrap class="deploy-content">
      <div class="form-section">
        <el-form label-width="auto" label-position="top" class="deploy-form">
          <el-form-item label="分配客服" class="form-item">
            <el-radio-group v-model="type" class="radio-group">
              <el-radio :label="1" size="large" class="radio-item">系统分配</el-radio>
              <el-radio :label="2" size="large" class="radio-item">指定客服</el-radio>
            </el-radio-group>
            <el-select v-model="assistantId" v-show="type == 2" class="select-assistant">
              <el-option
                v-for="item in assistantOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="是否使用大窗口居中显示" class="form-item">
            <el-radio-group v-model="center" class="radio-group">
              <el-radio :label="true" size="large" class="radio-item">使用大窗口（居中）</el-radio>
              <el-radio :label="false" size="large" class="radio-item"
                >使用小窗口（右下角）</el-radio
              >
            </el-radio-group>
          </el-form-item>

          <el-form-item
            label="是否自动拉起对话框: 选择否时会先创建一个联系客服的图标"
            class="form-item"
          >
            <el-radio-group v-model="autoshow" class="radio-group">
              <el-radio :label="true" size="large" class="radio-item">是</el-radio>
              <el-radio :label="false" size="large" class="radio-item">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>

      <el-tabs v-model="activeName" class="deploy-tabs" @tab-click="handleClick">
        <el-tab-pane label="网页内嵌" name="1"></el-tab-pane>
        <el-tab-pane label="超链接" name="2"></el-tab-pane>
      </el-tabs>

      <div v-if="activeName == 1" class="code-section">
        <div class="code-block">
          <p>(function () {</p>
          <p class="indent-1">_s = document.createElement("script");</p>
          <p class="indent-1">_s.src = "https://service-client.jzsaas.com/index.js";</p>
          <p class="indent-1">_s.onload = function () {</p>
          <p class="indent-2">const option = {</p>
          <p class="indent-3">
            <span class="option-key">center: {{ center }}, </span>
            <span class="comment"
              >// 是否使用大窗口居中显示, 默认为true 使用大窗口（居中），如果设置为false
              则会在页面右下角显示小窗口。</span
            >
          </p>
          <p class="indent-3">
            <span class="option-key">autoshow: {{ autoshow }}, </span>
            <span class="comment"
              >//
              是否自动拉起对话框，默认为true。如果设置为false将不会自动弹出会话框，而是在页面右下角创建一个"联系客服"按钮</span
            >
          </p>
          <p class="indent-3">
            <span class="option-key">tenantId: {{ getTenantId() }}, </span>
            <span class="comment">// 请勿修改此属性，否则客服无法生效</span>
          </p>
          <p class="indent-3">
            <span class="option-key">assistantId: '{{ assistantId }}',</span>
            <span class="comment">// 客服id, 未指定时将随机从在线客服中选择</span>
          </p>
          <p class="indent-2">};</p>
          <p class="indent-2">var canCustomerServer = new initCustomerServer(option);</p>
          <p class="indent-2">canCustomerServer.init();</p>
          <p class="indent-1">};</p>
          <p class="indent-1">document.head.appendChild(_s);</p>
          <p>})(); </p>
        </div>
        <el-alert type="success" class="alert-message" show-icon>
          复制此代码串到您的网站内即可自动创建在线客服标签
        </el-alert>
      </div>

      <div v-else class="link-section">
        <div class="link-block">
          <el-link class="generated-link">{{ text }}</el-link>
          <el-button type="primary" class="copy-btn" @click="onCopyThat">
            <el-icon><DocumentCopy /></el-icon>
            复制
          </el-button>
        </div>
        <el-alert type="success" class="alert-message" show-icon>
          将此链接复制到指定的客户按钮点击链接中
        </el-alert>
      </div>
    </ContentWrap>
  </div>
</template>

<script setup>
import * as AssistantApi from '@/api/service/assistant'
import { getTenantId } from '@/utils/auth'
import { config } from '@/config/biz'
import { DocumentCopy } from '@element-plus/icons-vue'
defineOptions({ name: 'AssistantDeploy' })
const route = useRoute()
const { query, params } = route

const activeName = ref('1')
const type = ref(1)

const assistantOptions = ref([])
const assistantId = ref(null)
const center = ref(true)
const autoshow = ref(true)

const text = computed(() => {
  return (
    config.service_client_url +
    '?center=' +
    center.value +
    '&autoshow=' +
    autoshow.value +
    '&tenantId=' +
    getTenantId() +
    '&assistantId=' +
    assistantId.value
  )
})

const onCopyThat = () => {
  navigator.clipboard.writeText(text.value)
}
watch(
  () => type.value,
  (nev) => {
    if (nev == 1) assistantId.value = null
  }
)

onMounted(async () => {
  assistantOptions.value = await AssistantApi.getAssistantList()
  const { id } = query
  if (id) {
    type.value = 2
    assistantId.value = parseInt(id)
  }
})
</script>

<style lang="scss" scoped>
.deploy-container {
  padding: 16px;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);

  @media (max-width: 768px) {
    padding: 8px;
  }
}

.deploy-content {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 82, 204, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  overflow: hidden;
  transition: all 0.3s ease;

  &:hover {
    box-shadow: 0 12px 40px rgba(0, 82, 204, 0.15);
  }
}

.form-section {
  padding: 24px;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.deploy-form {
  .form-item {
    margin-bottom: 24px;

    @media (max-width: 768px) {
      margin-bottom: 20px;
    }

    :deep(.el-form-item__label) {
      font-size: 16px;
      font-weight: 600;
      margin-bottom: 12px;
      color: #333333;
      @media (max-width: 768px) {
        font-size: 14px;
        margin-bottom: 8px;
      }
    }
  }
}

.radio-group {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 12px;
  }

  .radio-item {
    :deep(.el-radio__label) {
      font-size: 14px;
      font-weight: 500;
      color: #606266;

      @media (max-width: 768px) {
        font-size: 13px;
      }
    }

    :deep(.el-radio__input.is-checked .el-radio__inner) {
      background-color: #0052cc;
      border-color: #0052cc;
    }

    :deep(.el-radio__input.is-checked + .el-radio__label) {
      color: #0052cc;
      font-weight: 600;
    }

    &:hover {
      :deep(.el-radio__inner) {
        border-color: #0052cc;
      }
    }
  }
}

.select-assistant {
  width: 200px;
  margin-left: 10px;

  @media (max-width: 768px) {
    width: 100%;
    margin-top: 8px;
  }

  :deep(.el-select__wrapper) {
    border-radius: 8px;
    border: 2px solid #e4e7ed;
    transition: all 0.3s ease;

    &:hover {
      border-color: #0052cc;
    }

    &.is-focused {
      border-color: #0052cc;
      box-shadow: 0 0 0 2px rgba(0, 82, 204, 0.2);
    }
  }
}

.deploy-tabs {
  margin: 0 24px;

  @media (max-width: 768px) {
    margin: 0 16px;
  }

  :deep(.el-tabs__header) {
    margin-bottom: 20px;

    .el-tabs__nav-wrap {
      &::after {
        background-color: rgba(0, 82, 204, 0.1);
      }
    }

    .el-tabs__item {
      font-size: 16px;
      font-weight: 500;
      color: #606266;
      padding: 0 24px;
      height: 48px;
      line-height: 48px;
      transition: all 0.3s ease;

      @media (max-width: 768px) {
        font-size: 14px;
        padding: 0 16px;
        height: 44px;
        line-height: 44px;
      }

      &:hover {
        color: #0052cc;
      }

      &.is-active {
        color: #0052cc;
        font-weight: 600;
      }
    }

    .el-tabs__active-bar {
      background-color: #0052cc;
      height: 3px;
      border-radius: 2px;
    }
  }
}

.code-section,
.link-section {
  padding: 0 24px 24px;

  @media (max-width: 768px) {
    padding: 0 16px 16px;
  }
}

.code-block {
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border: 2px solid rgba(0, 82, 204, 0.1);
  border-radius: 12px;
  padding: 20px;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 12px;
  line-height: 1.6;
  color: #2c3e50;
  margin-bottom: 16px;
  overflow-x: auto;

  @media (max-width: 768px) {
    padding: 16px;
    font-size: 11px;
  }

  p {
    margin: 0;
    white-space: nowrap;

    @media (max-width: 768px) {
      white-space: pre-wrap;
      word-break: break-all;
    }
  }

  .indent-1 {
    padding-left: 20px;

    @media (max-width: 768px) {
      padding-left: 16px;
    }
  }

  .indent-2 {
    padding-left: 40px;

    @media (max-width: 768px) {
      padding-left: 32px;
    }
  }

  .indent-3 {
    padding-left: 60px;

    @media (max-width: 768px) {
      padding-left: 48px;
    }
  }

  .option-key {
    color: #0052cc;
    font-weight: 600;
    display: inline-block;
    min-width: 150px;

    @media (max-width: 768px) {
      min-width: auto;
      display: block;
      margin-bottom: 4px;
    }
  }

  .comment {
    color: #7f8c8d;
    font-style: italic;

    @media (max-width: 768px) {
      display: block;
      margin-left: 0;
    }
  }
}

.link-block {
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border: 2px solid rgba(0, 82, 204, 0.1);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 16px;

  @media (max-width: 768px) {
    flex-direction: column;
    align-items: stretch;
    padding: 16px;
    gap: 12px;
  }

  .generated-link {
    flex: 1;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 12px;
    color: #0052cc;
    word-break: break-all;
    padding: 8px 12px;
    background: rgba(255, 255, 255, 0.8);
    border-radius: 6px;
    border: 1px solid rgba(0, 82, 204, 0.2);

    @media (max-width: 768px) {
      font-size: 11px;
    }
  }

  .copy-btn {
    background: linear-gradient(135deg, #0052cc 0%, #003d99 100%);
    border: none;
    border-radius: 8px;
    padding: 10px 20px;
    font-weight: 600;
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
    transition: all 0.3s ease;

    @media (max-width: 768px) {
      width: 100%;
      padding: 12px;
    }

    &:hover {
      background: linear-gradient(135deg, #003d99 0%, #002966 100%);
      transform: translateY(-2px);
      box-shadow: 0 6px 16px rgba(0, 82, 204, 0.4);
    }

    &:active {
      transform: translateY(0);
    }

    .el-icon {
      margin-right: 6px;
    }
  }
}

.alert-message {
  border-radius: 10px;
  border: none;
  background: linear-gradient(135deg, rgba(103, 194, 58, 0.1) 0%, rgba(103, 194, 58, 0.05) 100%);

  :deep(.el-alert__icon) {
    color: #67c23a;
  }

  :deep(.el-alert__content) {
    color: #67c23a;
    font-weight: 500;

    @media (max-width: 768px) {
      font-size: 13px;
    }
  }
}

// 动画效果
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.code-section,
.link-section {
  animation: fadeInUp 0.6s ease-out;
}

// 深色模式适配
@media (prefers-color-scheme: dark) {
  .deploy-container {
    background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
  }

  .deploy-content {
    background: rgba(40, 40, 40, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.1);
  }

  .code-block,
  .link-block {
    background: linear-gradient(135deg, #2d2d2d 0%, #3a3a3a 100%);
    border-color: rgba(0, 82, 204, 0.3);
    color: #e0e0e0;
  }

  .generated-link {
    background: rgba(60, 60, 60, 0.8);
    color: #4da6ff;
  }
}

// 滚动条美化
:deep(.el-scrollbar__wrap) {
  scrollbar-width: thin;
  scrollbar-color: rgba(0, 82, 204, 0.3) transparent;

  &::-webkit-scrollbar {
    width: 6px;
    height: 6px;
  }

  &::-webkit-scrollbar-track {
    background: transparent;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 3px;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}
</style>
